//
//  MPBankInfoController.m
//  MarkPlatform
//
//  Created by xiao6 on 2018/3/9.
//  Copyright © 2018年 AT. All rights reserved.
//

#import "MPBankInfoController.h"
#import "MPBankSearchController.h"
#import "MPBankModel.h"
#import "MPInputCell.h"
#import "MPDetailCell.h"
#import "MPInputCellModel.h"
#import "MPBankPhoneController.h"

@interface MPBankInfoController ()
@property (nonatomic, strong) NSMutableArray<MPInputCellModel *> *dataList;
@property (nonatomic, strong) MPBankModel *bankModel;
@property (nonatomic, strong) MPAppButton *bottomBtn;
@end

@implementation MPBankInfoController
+ (instancetype)vcWithModel:(MPBankModel *)bankModel {
    MPBankInfoController *vc = [[self alloc] init];
    vc.bankModel = bankModel;
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showNavTitle:@"填写银行卡信息" backItem:YES];
    
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 400)];
    [footer addSubview:self.bottomBtn];
    [self.bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bottomBtn.superview.mas_left).offset(16);
        make.centerX.equalTo(self.bottomBtn.superview);
        make.top.equalTo(self.bottomBtn.superview).offset(60);
        make.height.equalTo(@(SCALEW(49.0f)));
    }];
    
    self.tableView.tableFooterView = footer;
    
    [MBProgressHUD graceShowMessage:nil toView:self.view completion:^(MBProgressHUD *hud) {
        [MPHttpTool GET:[NSString stringWithFormat:@"business/pay/queryCardBin/%@", self.bankModel.card] params:nil success:^(NSURLSessionDataTask *task, ATApiResult *result) {
            [hud hideAnimated:YES];
            NSDictionary *data = result.data[@"data"];
            if ([data isKindOfClass:[NSDictionary class]] && [data[@"bank_name"] length] > 0) {
                self.bankModel.name = data[@"bank_name"];
                self.bankModel.code = data[@"bank_code"];
                self.dataList.firstObject.inputText = self.bankModel.name;
                [self.tableView reloadData];
            }
            else {
                [ATAlertView showTitle:result.data[@"message"] message:nil normalButtons:nil highlightButtons:@[@"知道了"] completion:^(NSInteger index, NSString *buttonTitle) {
                    [self goBack];
                }];
            }
        } failure:ATHttpFailureWithHud(hud)];
    }];
}

#pragma mark - action
- (void)didClickBottomBtn:(id)sender {
    if ([self.dataList[0] inputText].length == 0) {
        [MBProgressHUD showMessage:@"请输入卡类型"];
        return;
    }
    if ([self.dataList[1] inputText].length < 11) {
        [MBProgressHUD showMessage:@"请输入银行卡预留手机号码"];
        return;
    }
    [self.view endEditing:YES];
    
    [self.navigationController pushViewController:[MPBankPhoneController vcWithModel:self.bankModel] animated:YES];
}

#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15.0f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MPInputCell *cell = [MPInputCell cellForTableView:tableView indexPath:indexPath];
    cell.model = self.dataList[indexPath.row];
    cell.userInteractionEnabled = indexPath.row == 1;
    @weakify(self)
    cell.notifyInputChange = ^(MPInputCell *cell) {
        @strongify(self)
        if (indexPath.row == 1) {
            self.bankModel.tel = cell.model.inputText;
        }
    };
    return cell;
}

#pragma mark - getter
- (NSMutableArray<MPInputCellModel *> *)dataList {
    if (!_dataList) {
        _dataList = [NSMutableArray array];
        [_dataList addObject:[MPInputCellModel modelWithTitle:@"卡类型" placeholder:@"请输入卡类型" inputText:self.bankModel.name]];
        [_dataList addObject:[MPInputCellModel modelWithTitle:@"手机号" placeholder:@"请输入银行卡预留手机号码" inputText:self.bankModel.tel maxLength:11 keyboardType:UIKeyboardTypeNumberPad]];
    }
    return _dataList;
}
- (UIButton *)bottomBtn {
    if (!_bottomBtn) {
        _bottomBtn = [MPAppButton buttonWithTitle:@"绑卡" target:self action:@selector(didClickBottomBtn:)];
    }
    return _bottomBtn;
}
@end
